#include <stdio.h>
#include <string.h>

#define MAX_LEN  100

size_t endindex(char *str, unsigned dist) {
    size_t len = strlen(str);

    if (len > 1 && dist > 0 && dist < len) {
        return --len;
    }

    return 0;
}


char* common(char *str, unsigned d){
    size_t l = endindex(str, d);
    if (!l) {
        return str;
    }

    char tmp[MAX_LEN] = {0};
    unsigned i = 0;
    unsigned j = d;

    //缓存前dist长度的字符串
    while (j--) {
        tmp[i] = str[i];
        ++i;
    }

    //将后面字符串前移
    while (i - l - 1) {
        str[i-d] = str[i++];
    }

    //将tmp中的字符串接到后面
    while (d--) {
        str[l--] = tmp[d];
    }

    return str;
}

char* recusize(char *str, unsigned d){
    size_t l = endindex(str, d);
    if (!l) {
        return str;
    }

    while (d--) {
        char tmp = *str;
        int i = 1;

        for ( ; i <= l; ++i) {
            str[i-1] = str[i];
        }
        str[l] = tmp;
    }

    return str;
}

char* acrobatics(char *str, unsigned d){
    size_t l = endindex(str, d);
    if (!l) {
        return str;
    }

    unsigned i = d;

    for (; i<=l; i++) {
        unsigned j = 0;

        while (1) {

            str[i*(1<<j) - d] = str[i*(1<<j)];

            ++j;


            if (i*(1<<j) > l) {
                break;
            }

            puts(str);
        }

    }
}

void main(void){
    char str[MAX_LEN] = "abcdef";
    unsigned dist = 2;

    char str2[MAX_LEN];
    char str3[MAX_LEN];
    char str4[MAX_LEN];

    strcpy(str2, str);
    strcpy(str3, str);
    strcpy(str4, str);

    printf("string: %s len: %d\n", str, dist);
    puts(common(str, dist));

    puts("\nrecuvise ways:\n");
    printf("string: %s len: %d\n", str2, dist);
    puts(recusize(str2, dist));


    puts("\n acrobatics ways:\n");
    printf("string: %s len: %d\n", str3, dist);
    puts(acrobatics(str3, dist));
}
